package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.edu.bean.Admin;
import com.edu.bean.Orders;
import com.edu.bean.User;
import com.edu.dao.AdminDao;
import com.edu.dao.BaseDao;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin selectAll(String username, String password) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Admin admin = null;
		String sql = "SELECT * FROM administrator WHERE username = ? and password = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			res = ps.executeQuery();
			if(res.next()) {
				admin = new Admin();
				admin.setUserId(res.getInt("userId"));
				admin.setUsernaem(res.getString("username"));
				admin.setPassword(res.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return admin;
	}

	@Override
	public int selectAllusers() {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int result = 0;
		String sql = "SELECT count(1) as total FROM user";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				result = res.getInt("total");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return result;
	}

	@Override
	public Admin selectadminname(String username) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Admin admin = null;
		String sql = "SELECT * FROM administrator WHERE username = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			res = ps.executeQuery();
			if(res.next()) {
				admin = new Admin();
				admin.setUserId(res.getInt("userId"));
				admin.setUsernaem(res.getString("username"));
				admin.setPassword(res.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return admin;
	}

	

}
